package com.andnnl.list;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.andnnl.future.ThreadUtil;
import com.xkzhangsan.time.calculator.DateTimeCalculatorUtil;
import com.xkzhangsan.time.formatter.DateTimeFormatterUtil;

import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * Created by chenss on 2022/11/16.
 */
public class ForEachTest {
    static AtomicInteger count=new AtomicInteger();
    static AtomicInteger addcount=new AtomicInteger();
    public static void main(String[] args) {
        // Person 对象
        test1();

        test2();
        while (true) {
            ThreadUtil.sleep(1000);
        }
    }

    private static void test1() {
        // 根据name去重
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("1", "name1"));
        persons.add(new Person("2", "name1"));
        persons.add(new Person("3", "name3"));
        persons.add(new Person("4", "name4"));
        List<Person> list = persons.stream().filter(p -> p.getId().equals("2")).collect(Collectors.toList());
        list.add(0,new Person("4", "name4"));
        System.out.println(list);

        // 根据name去重
        List<Person> persons2 = new ArrayList<>();
        persons2.add(new Person("1", "name111111"));
        persons2.add(new Person("2", "name111112"));
        persons2.add(new Person("3", "name111113"));

        persons.stream().forEach(c -> {
            persons2.stream().filter(x -> Objects.equals(x.getId(), c.getId())).forEach(x -> c.setName(x.getName()));
        });

        System.out.println(JSON.toJSON(persons));
        System.out.println(JSON.toJSON(persons2));
        Date d = DateTimeCalculatorUtil.plus(new Date(), ChronoUnit.HOURS, -4);
        System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(d));
    }

    private static void test2() {
//        List<String> list = Collections.synchronizedList(new ArrayList<>());
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
//        ArrayList<String> list = new ArrayList<>();

// 添加元素
        new Thread(() -> {
            for (int i = 0; i < 100000; i++) {
                list.add("Item " + i);
                addcount.incrementAndGet();
            }
        }).start();

        removeI(list);

//        System.out.println(list.size());
        while (true){
            ThreadUtil.sleep(2000);
            if(1==1)System.out.println("list.size()  "+list.size() +" count:"+count.get() +" add:"+addcount.get());
        }
    }

    private static void removeI(List<String> list) {
        for (int i = 0; i < 10; i++) {
            // 遍历列表
            new Thread(() -> {
                while (true) {

                    try {
//                        synchronized (list) {
                            for (String item : list) {
//                                System.out.println(item);
                            }
//                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if(1==2)break;
//                    final int index = RandomUtil.randomInt(list.size());
//                    System.out.println(index +"  size:"+list.size());

//                    list.remove(index);
//                    synchronized (list) {
                        if (list.size() > 0) {
                            try {
                                System.out.println(list.size());
                                final int index = RandomUtil.randomInt(list.size());
                                list.remove(index);
                                count.incrementAndGet();
//                                for (int j = 0; j < list.size(); j++) {
//                                    list.remove(j);
//                                }
/*                                Iterator<String> it = list.iterator();
                                while (it.hasNext()) {
                                    it.remove();
                                }*/
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
//                            System.out.println("finish");
//                            break;
                        }
//                    }
                }

            }).start();
        }
    }
}
